package com.situ.swing111;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class MainFrame extends JFrame {
	private StudentTableModel studentTableModel;
	private JTable table;
	private List<Student> list;
	
	
	public static void main(String[] args) {
		MainFrame mainFrame = new MainFrame();
		mainFrame.init();
		
	}
	
	
	
	public void init() {
		this.setSize(800, 600);
		// mainFrame.setLocation(300, 50);
		SwingUtil.setLocationCenter(this);
//		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//只销毁本窗口

		JPanel mainPanel = (JPanel) this.getContentPane();
		BoxLayout boxLayout = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
		mainPanel.setLayout(boxLayout);
		// 窗口从上往下第一部分
		JPanel topPanel = new JPanel();
		// 设置布局之间的间隙
		topPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
		// 姓名
		JLabel nameLabel = new JLabel("姓名： ");
		JTextField nameText = new JTextField();
		nameText.setPreferredSize(new Dimension(130, 30));
		topPanel.add(nameLabel);
		topPanel.add(nameText);
		// 搜索按钮
		JButton searchButton = new JButton("搜索");
		searchButton.setPreferredSize(new Dimension(80, 30));
		topPanel.add(searchButton);

		// 窗口从上往下第二部分，主要是table表格
		JPanel centerPanel = new JPanel();
		centerPanel.setBackground(Color.BLACK);

		table = new JTable();
		table.setRowHeight(45);
		List<Student> list = loadData();

		studentTableModel = new StudentTableModel(list);
		table.setModel(studentTableModel);
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setPreferredSize(new Dimension(300, 50));
		centerPanel.setLayout(new BorderLayout());
		centerPanel.add(scrollPane, BorderLayout.CENTER);

		// 窗口从上往下第三部分, 新增、修改、删除三个按钮
		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 80, 10));
		// addButton
		JButton addButton = new JButton("新增");
		addButton.setPreferredSize(new Dimension(80, 30));
		addButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				AddStudentFrame addStudentFrame = new AddStudentFrame(MainFrame.this);
				addStudentFrame.init();
			}
		});
		bottomPanel.add(addButton);
		// updateButton
		JButton updateButton = new JButton("修改");
		updateButton.setPreferredSize(new Dimension(80, 30));
		bottomPanel.add(updateButton);
		// deleteButton
		JButton deleteButton = new JButton("删除");
		deleteButton.setPreferredSize(new Dimension(80, 30));
		deleteButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// 选中的所有行的索引
				int[] indices = table.getSelectedRows();
				if (indices.length == 0) {// 未选中任何行
					JOptionPane.showMessageDialog(MainFrame.this, "请选择您要删除的行", "提示", JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				for (int i = 0; i < indices.length; i++) {
					Integer id = (Integer) table.getValueAt(indices[i], 0);
					Iterator<Student> iterator = list.iterator();
					while (iterator.hasNext()) {
						Student student = iterator.next();
						if (student.getId() == id) {
							iterator.remove();
						}
					}
				}

				MainFrame.this.reloadTable();
			}
		});
		bottomPanel.add(deleteButton);

		mainPanel.add(topPanel);
		mainPanel.add(centerPanel);
		mainPanel.add(bottomPanel);

		this.setVisible(true);
	}

	private List<Student> loadData() {
		list = new ArrayList<>();
		Student student1 = new Student(180701, "张三1", 20, "女",70);
		Student student2 = new Student(180702, "张三2", 21, "男",89);
		Student student3 = new Student(180703, "张三3", 24, "男",69);
		Student student4 = new Student(180704, "张三4", 22, "女",90);
		Student student5 = new Student(180705, "张三5", 24, "男",67);
		Student student6 = new Student(180706, "张三6", 22, "女",55);
		Student student7 = new Student(180707, "张三7", 25, "男",78);
		Student student8 = new Student(180708, "张三8", 23, "女",76);
		Student student9 = new Student(180709, "张三9", 25, "男",89);
		list.add(student1);
		list.add(student2);
		list.add(student3);
		list.add(student4);
		list.add(student5);
		list.add(student6);
		list.add(student7);
		list.add(student8);
		list.add(student9);
		return list;
	}
	
	public List<Student> getData() {
		return list;
	}
	
	/*
	 * 重新加载表格数据
	 */
	public void reloadTable() {
		studentTableModel.setData(list);
		studentTableModel.fireTableDataChanged();// 触发表格数据变更事件
	}
}
